/**
 * HistoryQueryServiceImpl.java
 * Created at 2017-6-29
 * Created by xieshuang
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.monitor.service.impl;

import com.broadtext.common.exp.DaoException;
import com.broadtext.common.repository.EntityDao;
import com.broadtext.common.service.impl.EntityServiceImpl;
import com.broadtext.common.utils.StringUtils;
import com.broadtext.rms.basic.action.TaskCallable;
import com.broadtext.rms.monitor.model.HistoryQueryView;
import com.broadtext.rms.monitor.service.IHistoryQueryService;
import com.broadtext.rms.monitor.thread.QueryTempThread;
import com.broadtext.rms.monitor.thread.QueryVolThread;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * <p>ClassName: HistoryQueryServiceImpl</p>
 * <p>Description: 历史查询服务接口实现类</p>
 * <p>Author: xieshuang</p>
 * <p>Date: 2017-6-29</p>
 */
@Service("historyQueryService")
public class HistoryQueryServiceImpl extends EntityServiceImpl<HistoryQueryView> implements IHistoryQueryService {
    
	@Autowired
    @Qualifier("historyQueryDao")
    public void setEntityDao(EntityDao<HistoryQueryView> entityDao) {
        super.entityDao = entityDao;
    }
	
	/**
	 * 
	 * <p>Description: 获取历史数据分析表格data</p>
	 * @param paramsMap 查询条件
	 * @return 结果集
	 * @throws DaoException 
	 */
	@SuppressWarnings("unchecked")
	public Object getHistoryChartData(Map<String, Object> paramsMap) throws DaoException {
		if (paramsMap!=null&&paramsMap.get("packCode")!=null) {
			List<String> clientIds = this.entityDao.findByQuery("com.broadtext.rms.monitor.model.TtPackHis.getColClientId", paramsMap);
			if (clientIds!=null&&clientIds.size()>0&& StringUtils.isNotBlank(clientIds.get(0))) {
				paramsMap.put("colClientId",clientIds.get(0));
			}
		}
        List<Map<String, Object>> charInfo = new ArrayList<Map<String, Object>>();
        long starTime=System.currentTimeMillis();
		if("7".equals(paramsMap.get("type"))){
			List<String> groupNoList;
			groupNoList = this.entityDao.findByQuery("com.broadtext.rms.monitor.model.TtCellvoltageCurrent.getGroupNoList", paramsMap);
			if (null != groupNoList && !groupNoList.isEmpty()) {
				
				//每一电池id下面含有的组
				for (int i = 0; i < groupNoList.size(); i++) {
					Map<String, Object> groupData = new HashMap<String, Object>();
	                paramsMap.put("volGroupNo", groupNoList.get(i));
	                List<String> groupCoreNoList;
	                groupCoreNoList  = this.entityDao.findByQuery("com.broadtext.rms.monitor.model.TtCellvoltageCurrent.getGroupCoreNoList", paramsMap);
	                
	                List<Map<String, Object>> groupNoData = new ArrayList<Map<String, Object>>();
	                if (null != groupCoreNoList && !groupCoreNoList.isEmpty()) {
	                	
	                	//每一组下面的单体
	                    ExecutorService exec = Executors.newCachedThreadPool();//工头
	                    ArrayList<Future<List<HistoryQueryView>>> results = new ArrayList<Future<List<HistoryQueryView>>>();//
	                	for (int k = 0; k < groupCoreNoList.size(); k++) {
	                		//启动线程查询数据
	                	    Map<String, Object> conCurrentMap = new ConcurrentHashMap<>();
	                	    for (String key : paramsMap.keySet()) {
	                	        conCurrentMap.put(key, paramsMap.get(key));
                            }
	                	    conCurrentMap.put("volGroupCoreNo", groupCoreNoList.get(k));
	                	    //params.put("volGroupCoreNo", groupCoreNoList.get(k));
	                		QueryVolThread t = new QueryVolThread(conCurrentMap,entityDao);
	                		results.add(exec.submit(t));
						}
	                	for (int m = 0; m < results.size(); m++) {
	                	    Future<List<HistoryQueryView>> future = results.get(m);
	                	    Map<String, Object> singleDataMap = new HashMap<String, Object>();
	                	    boolean flag = true;
                            while(flag){
                                if(future.isDone()){
                                    flag = false;
                                    try {
                                        List<HistoryQueryView> singleDataList = future.get();
                                        if (null != singleDataList && !singleDataList.isEmpty()) {
                                            List<String> xData = new ArrayList<String>();
                                            List<String> yData = new ArrayList<String>();
                                            for (int j = 0; j < singleDataList.size(); j++) {
                                                xData.add(singleDataList.get(j).getGatherTime());
                                                yData.add(singleDataList.get(j).getMinVol());
                                            }
                                            singleDataMap.put("xData", xData);
                                            singleDataMap.put("yData", yData);
                                            singleDataMap.put("coreNo", groupCoreNoList.get(m));
                                            groupNoData.add(singleDataMap);
                                            
                                        }
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    } catch (ExecutionException e) {
                                        e.printStackTrace();
                                    }
                                }
                                
                            }
                        }
	                	groupData.put("volGroupNo", groupNoList.get(i));
	                	groupData.put("data", groupNoData);
	                	charInfo.add(groupData);
	                }
				}
			}
			long endTime=System.currentTimeMillis();
	        long time=endTime-starTime;
	        System.out.println("查询总共用时："+time);
			return charInfo;
		} else if("6".equals(paramsMap.get("type"))){
			List<String> groupNoList;
			groupNoList = this.entityDao.findByQuery("com.broadtext.rms.monitor.model.TtSamplingpointTempCurrent.getGroupNoList", paramsMap);
			if (null != groupNoList && !groupNoList.isEmpty()) {
				
				//每一电池id下面含有的组
				for (int i = 0; i < groupNoList.size(); i++) {
					Map<String, Object> groupData = new HashMap<String, Object>();
	                paramsMap.put("volGroupNo", groupNoList.get(i));
	                List<String> groupCoreNoList;
	                groupCoreNoList  = this.entityDao.findByQuery("com.broadtext.rms.monitor.model.TtSamplingpointTempCurrent.getGroupCoreNoList", paramsMap);
	                
	                List<Map<String, Object>> groupNoData = new ArrayList<Map<String, Object>>();
	                if (null != groupCoreNoList && !groupCoreNoList.isEmpty()) {
	                    
	                    //每一组下面的单体
                        ExecutorService exec = Executors.newCachedThreadPool();//工头
                        ArrayList<Future<List<HistoryQueryView>>> results = new ArrayList<Future<List<HistoryQueryView>>>();//
                        for (int k = 0; k < groupCoreNoList.size(); k++) {
                            //启动线程查询数据
                            Map<String, Object> conCurrentMap = new ConcurrentHashMap<>();
                            for (String key : paramsMap.keySet()) {
                                conCurrentMap.put(key, paramsMap.get(key));
                            }
                            conCurrentMap.put("volGroupCoreNo", groupCoreNoList.get(k));
                            //params.put("volGroupCoreNo", groupCoreNoList.get(k));
                            QueryTempThread t = new QueryTempThread(conCurrentMap,entityDao);
                            results.add(exec.submit(t));
                        }
                        for (int m = 0; m < results.size(); m++) {
                            Future<List<HistoryQueryView>> future = results.get(m);
                            Map<String, Object> singleDataMap = new HashMap<String, Object>();
                            boolean flag = true;
                            while(flag){
                                if(future.isDone()){
                                    flag = false;
                                    try {
                                        List<HistoryQueryView> singleDataList = future.get();
                                        if (null != singleDataList && !singleDataList.isEmpty()) {
                                            List<String> xData = new ArrayList<String>();
                                            List<String> yData = new ArrayList<String>();
                                            for (int j = 0; j < singleDataList.size(); j++) {
                                                xData.add(singleDataList.get(j).getGatherTime());
                                                yData.add(singleDataList.get(j).getMinVol());
                                            }
                                            singleDataMap.put("xData", xData);
                                            singleDataMap.put("yData", yData);
                                            singleDataMap.put("coreNo", groupCoreNoList.get(m));
                                            groupNoData.add(singleDataMap);
                                            
                                        }
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    } catch (ExecutionException e) {
                                        e.printStackTrace();
                                    }
                                }
                                
                            }
                        }
	                	
	                	//每一组下面的单体
	                	/*for (int k = 0; k < groupCoreNoList.size(); k++) {
	                		Map<String, Object> singleDataMap = new HashMap<String, Object>();
	                		paramsMap.put("volGroupCoreNo", groupCoreNoList.get(k));
	                		List<HistoryQueryView> singleDataList;
		                	singleDataList = this.entityDao.findByQuery("com.broadtext.rms.monitor.model.TtSamplingpointTempCurrent.getVolListByCondition", paramsMap);
		                	
		                	if (null != singleDataList && !singleDataList.isEmpty()) {
		                		List<String> xData = new ArrayList<String>();
		                		List<String> yData = new ArrayList<String>();
		                		for (int j = 0; j < singleDataList.size(); j++) {
		                			xData.add(singleDataList.get(j).getGatherTime());
		                			yData.add(singleDataList.get(j).getMinVol());
								}
		                		singleDataMap.put("xData", xData);
		                		singleDataMap.put("yData", yData);
		                		singleDataMap.put("coreNo", groupCoreNoList.get(k));
		                		groupNoData.add(singleDataMap);
		                		
		                	}
						}*/
	                	groupData.put("volGroupNo", groupNoList.get(i));
	                	groupData.put("data", groupNoData);
	                	charInfo.add(groupData);
	                }
				}
			}
			long endTime=System.currentTimeMillis();
            long time=endTime-starTime;
            System.out.println("查询总共用时："+time);
			return charInfo;
		} else {
			//其他曲线
			
			return this.entityDao.findByQuery("com.broadtext.rms.monitor.model.TtPackHis.getHistoryChartData", paramsMap);
		}
		
	}
	
	/**
	 * 
	 * <p>Description: 导出历史数据</p>
	 * @return 结果集
	 * @throws DaoException 
	 */
	@SuppressWarnings("unchecked")
	public List<Map<String, String>> findExportHistoryDataList(Map<String, Object> params) throws DaoException {
		return  this.entityDao.findByQuery("com.broadtext.rms.monitor.model.TtPackHis.findExportHistoryData", params);
	}
	
	public static void main(String[] args) {
	    ExecutorService exec = Executors.newCachedThreadPool();//工头
	    ArrayList<Future<String>> results = new ArrayList<Future<String>>();//
	    for(int i = 0 ; i < 10 ;i++){
	        results.add(exec.submit(new TaskCallable(i)));//submit返回一个Future，代表了即将要返回的结果
	    }
    }
}
